package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.UserMapper;
import com.heima.admin.service.LoginService;
import com.heima.model.admin.dto.UserDto;
import com.heima.model.admin.pojo.AdUser;
import com.heima.model.admin.vo.AdUserVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @作者 itcast
 * @创建日期 2021/5/23 15:27
 **/
@Service
public class LoginServiceImpl extends ServiceImpl<UserMapper, AdUser> implements LoginService {
    @Override
    public ResponseResult login(UserDto dto) {
        //1.检查用户
        if (StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码有误");
        }
        //2.根据用户名查询user对象
        //查询条件
        LambdaQueryWrapper<AdUser> wrapper = Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, dto.getName());
        AdUser user = this.getOne(wrapper);
        if (null ==user){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }
        //3.校验密码   MD5加密         (  前台传的密码   +   数据库中的盐  )
        String inPassword = DigestUtils.md5DigestAsHex((dto.getPassword() + user.getSalt()).getBytes());
        if (!inPassword.equals(user.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //4.检验用户状态
        if (user.getStatus()!=9){   //9位有效状态
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户状态无效");
        }
        //5.设置登录时间
        user.setLoginTime(new Date());
        updateById(user);
        //6.获取token
        //String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
        String token = AppJwtUtil.getToken(user.getId());
        //将token放到map中
        Map map = new HashMap();
        map.put("token",token);
        AdUserVo userVo = new AdUserVo();
        BeanUtils.copyProperties(user,userVo);
        map.put("user",userVo);
        //7.响应
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult register(AdUser user) {
        //1.判断用户名和密码
        if (StringUtils.isBlank(user.getName())||StringUtils.isBlank(user.getPassword())){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码输入有误");
        }
        String salt = RandomStringUtils.randomAlphanumeric(8);
        String password = user.getPassword();
        String pass = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        user.setPassword(pass);
        user.setSalt(salt);
        user.setCreatedTime(new Date());
        user.setStatus(9);
        this.save(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
